$(function() {

	var code; //定义全局变量接收验证码
	//发送验证码
	if ($('#registeremail').val() == "") {
		$('#emialcore').prop("disabled", true); //按键不可用
	}
	$('#registeremail').change(function() {
		if ($('#registeremail').val() == "" || !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($('#registeremail').val())) {
			$('#emialcore').prop("disabled", true); //按键不可用
			//如果为空或格式不正确
		} else if ($('#registeremail').val() != "") {
			$('#emialcore').prop("disabled", false); //按键可用
		}
	})

//发送验证码
	$('#emialcore').click(function(event) {
		event.preventDefault()
		if ($('#registeremail').val() != "") {
			var time = 60; //定义60秒的倒计时
			var registeremail = $('#registeremail').val(); //获取输入框的邮箱
			
			//发送邮箱
			$.ajax({
				url: "v1/users/email",
				type: "get",
				async: false,
				data: {
					email: registeremail
				},
				success: function(data) {
					// console.log(data.status)
				},
				error: function(err) {
					console.log(err);
				},
			});
			
			//获取验证码
			code = (function() {
				var result;
				$.ajax({
					type: 'get',
					url: 'v1/users/emailyzm',
					async: false,
					data: {
						email: registeremail
					},
					success: function(data) {
						result = data.data;
					}
				});
				return result;
			})();
			// console.log(code)

			//设置一个定时，一秒执行一次
			var mytime = setInterval(function() {
				subs();
			}, 1000)


			function subs() {
				time--;
				$('#emialcore').attr("value", "请" + time + "秒后再试");
				if (time === 0) {
					clearInterval(mytime);
					$('#emialcore').attr("value", "发送验证码");
					$('#emialcore').prop("disabled", false); //按键可用
				} else {
					$('#emialcore').prop("disabled", true); //按键不可用
				}
			}
		}

	})








	var flag = true; //标志
	//提交前表单验证，提交数据也可以通过from表单的action不过要给input设置name，后端获取到的就是name
	$('#registersubmit').click(function(event) {
		event.preventDefault()
		let registerusername = $("#registerusername").val(); //用户名
		let registerpassword = $("#registerpassword").val(); //密码
		let password2 = $("#surePassword").val(); //确认密码
		let registerphone = $('#registerphone').val(); //获取手机号
		let registertype = $('#registeruserType').val(); //获取类型
		let sex = $("#userSex").val(); //获取性别
		let birthday = $('#birthday').val(); //获取生日
		let registeremail = $('#registeremail').val(); //获取邮箱
		let registeremailcode = $('#registercode').val(); //获取邮箱验证码
		let reg = /[aA][vV]/ //敏感词


		if (registerusername == "") { //判断用户名是否填写
			$("#registername_trip").show(); //显示标签
			flag = false;
		} else if (registerpassword == "") { //判断密码是否填写
			$("#registerpassword_trip").show();
			flag = false;
		} else if (password2 == "") { //判断密码是否填写
			$("#surePassword_trip").show();
			flag = false;
		} else if (registerphone == "") { //判断是否填写手机号
			$('#registerphone_trip').show();
			flag = false;
		} else if (registertype == "") { //判断是否选择类型
			$("#registertype_trip").show();
			flag = false;
		} else if (sex == "") { //判断是否选择性别
			$("#sex_trip").show();
			flag = false;
		} else if (birthday == "") { //判断是否选择生日
			$("#birthday_trip").show();
			flag = false;
		} else if (registeremail == "") { //判断密码是否填写邮箱
			$("#registeremail_trip").show();
			flag = false;
		} else if (registeremailcode == "") { //判断是否填写验证码
			$('#registeremailcode_trip').show();
			flag = false;
		} else if (/^\d+$/.test(registerusername) && registerusername != "") { //判断用户名是否为纯数字
			flag = false;
		} else if (registerusername.search(reg) != -1) { //判断用户名是否有敏感词
			flag = false;
		} else if (registerpassword.length < 8 || /^\d+$/.test(registerpassword)) { //判断密码是否为纯数字
			flag = false;
		} else if (password2 != registerpassword && password2 != "") { //判断两次输入的密码是否一致
			flag = false;
		} else if (!/^1[3-9]\d{9}$/.test(registerphone)) { //判断手机号格式是否正确
			flag = false;
		} else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(registeremail)) { //判断邮箱格式是否正确
			flag = false;
		} else if (registeremailcode != code && registeremailcode != "") { //判断邮箱验证码是否正确
			flag = false;
		} else {
			$.ajax({
				url: "v1/users/register",
				type: "post",
				async: false,
				data: {
					username: $("#registerusername").val(),
					password: $("#registerpassword").val(),
					phone: $("#registerphone").val(),
					type: $("#registeruserType").val(),
					sex: $("#userSex").val(),
					birthday: $("#birthday").val(),
					email: $("#registeremail").val()
				},
				success: function(data) {

					if (data.status == 200) {
						alert('注册成功');
						window.location = '/'
					} else {
						alert('注册失败');
					}

				},
				error: function(err) {
					console.log(err);
					alert('注册失败,请检查用户名，手机号，邮箱是否可用');
				},
			});
		}
		return flag; //返回标志
	});




	// //重置
	// $('#reset').click(function() {
	// 	let registerpassword = $("#registerpassword").val(); //密码
	// 	let password2 = $("#surePassword").val(); //确认密码
	// 	let phone = $('#phone').val(); //获取手机号
	// 	let type = $('#userType').val(); //获取类型
	// 	let sex = $("#userSex").val(); //获取性别
	// 	let birthday = $('#birthday').val(); //获取生日
	// 	let email = $('#email').val(); //获取邮箱
	// 	let emailcode=$('#code').val();//获取邮箱验证码
	// 	registerpassword == "";
	// 	password2 == "";
	// 	phone == "";
	// 	type == "";
	// 	sex == "";
	// 	birthday == "";
	// 	email == "";
	// 	emailcode=="";
	// })



	//输入框改变
	$('#registerusername').change(function() {
		let registerusername = $('#registerusername').val();
		//敏感词判断
		var reg = /[aA][vV]/;
		if (/^\d+$/.test(registerusername) && registerusername != "") { //判断是否为数字
			$('#registername_number').show();
			$('#registername_trip').hide();
			$('#registername_check').hide();
			$('#registername_check1').hide();
			$('#registername_minggan').hide();

		} else if (registerusername.search(reg) != -1 && registerusername != "") {
			$('#registername_minggan').show();
			$('#registername_number').hide();
			$('#registername_trip').hide();
			$('#registername_check').hide();
			$('#registername_check1').hide();
		} else if (registerusername == "") {
			$('#registername_trip').show();
			$('#registername_check1').hide();
			$('#registername_number').hide();
			$('#registername_check').hide();
			$('#registername_minggan').hide();
		} else {
			$('#registername_number').hide();
			$('#registername_trip').hide();
			$('#registername_check').hide();
			$('#registername_check1').hide();
			$('#registername_minggan').hide();
			$.ajax({
				url: "v1/users/checkname",
				type: "post",
				data: {
					username: $("#registerusername").val()
				},
				success: function(data) {
					if (data.status == 200) {
						$('#registername_check').show();
						$('#registername_check1').hide();
					} else {
						$('#registername_check1').show();
						$('#registername_check').hide();
					}
				},
				error: function(err) {
					console.log(err)
				},
			});
		}
	})





	//密码改变
	$('#registerpassword').change(function() {
		let registerpassword = $('#registerpassword').val();
		if (registerpassword == "") {
			$('#registerpassword_trip').show();
		} else {
			$('#registerpassword_trip').hide();
		}


		if ((registerpassword.length < 8 || /^\d+$/.test(registerpassword)) && registerpassword !=
			"") { //判断是否为数字
			$('#registerpassword_warn').show();
		} else {
			$('#registerpassword_warn').hide();
		}
	})


	//确认密码改变
	$('#surePassword').change(function() {
		let password2 = $('#surePassword').val();
		let registerpassword = $('#registerpassword').val();
		if (password2 != registerpassword && password2 != "") { //判断两次输入的密码是否一致
			$('#Errors').show();

		} else {
			$('#Errors').hide();
		}

		if (password2 == "") {
			$('#surePassword_trip').show();
		} else {
			$('#surePassword_trip').hide();
		}
	})


	//手机号输入框改变
	$('#registerphone').change(function() {
		let registerphone = $('#registerphone').val();
		if (registerphone == "") {
			$('#registerphone_trip').show();
			$('#registerphone_check').hide();
			$('#registerphone_check1').hide();
			$('#registerphone_error').hide();
		} else if (!/^1[3-9]\d{9}$/.test(registerphone) && registerphone != "") {
			$('#registerphone_error').show();
			$('#registerphone_trip').hide();
			$('#registerphone_check').hide();
			$('#registerphone_check1').hide();
		} else {
			$('#registerphone_error').hide();
			$('#registerphone_trip').hide();
			$('#registerphone_check').hide();
			$('#registerphone_check1').hide();
			//验证手机是否已存在
			$.ajax({
				url: "v1/users/checkphone",
				type: "post",
				data: {
					phone: $('#registerphone').val()
				},
				success: function(data) {
					if (data.status == 200) {
						$('#registerphone_check').show();
						$('#registerphone_check1').hide();
					} else {
						$('#registerphone_check1').show();
						$('#registerphone_check').hide();
					}
				},
				error: function(err) {
					console.log(err)
				},
			});


		}
	})



	$('#registeruserType').change(function() {
		let registertype = $('#registeruserType').val();
		if (registertype == "") {
			$('#registertype_trip').show();
		} else {
			$('#registertype_trip').hide();
		}
	})

	$('#userSex').change(function() {
		let sex = $('#userSex').val();
		if (sex == "") {
			$('#sex_trip').show();
		} else {
			$('#sex_trip').hide();
		}
	})

	$('#birthday').change(function() {
		let birthday = $("#birthday").val(); //赋值
		if (birthday == "") {
			$('#birthday_trip').show();
		} else {
			$('#birthday_trip').hide();
		}
	})


	//邮箱输入框改变
	$('#registeremail').change(function() {
		let registeremail = $('#registeremail').val();
		if (registeremail == "") {
			$('#registeremail_trip').show();
			$('#registeremail_check').hide();
			$('#registeremail_check1').hide();
			$('#registeremail_error').hide();
		} else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(registeremail) && registeremail !="") { //判断邮箱格式是否正确
			$('#registeremail_error').show();
			$('#registeremail_check1').hide();
			$('#registeremail_check').hide();
			$('#registeremail_trip').hide();
		} else {
			$('#registeremail_trip').hide();
			$('#registeremail_check').hide();
			$('#registeremail_check1').hide();
			$('#registeremail_error').hide();
			//验证邮箱是否已存在
			$.ajax({
				url: "v1/users/checkemail",
				type: "post",
				data: {
					email: $('#registeremail').val()
				},
				success: function(data) {
					if (data.status == 200) {//邮箱已存在
						$('#registeremail_check').show();
						$('#registeremail_check1').hide();
						$('#emialcore').prop("disabled", true); //按键不可用
					} else {
						$('#registeremail_check1').show();
						$('#registeremail_check').hide();
						$('#emialcore').prop("disabled", false); //按键可用
					}
				},
				error: function(err) {
					console.log(err)
				},
			});
		}
	})


	$('#registercode').change(function() {
		let registeremailcode = $('#registercode').val();

		if (registeremailcode != code && registeremailcode != "") {
			$('#registeremailcode_error').show();
			$('#registeremailcode_trip').hide();
		} else {
			$('#registeremailcode_error').hide();
		}

		if (registeremailcode == "") {
			$('#registeremailcode_trip').show();
		} else {
			$('#registeremailcode_trip').hide();
		}

	})









})
